package com.wzr.furerp.excel;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractXlsxStreamingView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

public class MyXlsxView extends AbstractXlsxStreamingView {

	@Override
	protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest req,
									  HttpServletResponse resp) throws Exception {
		String postInfo = req.getParameter("postInfo");
		Gson json = new Gson();
		Map joPostInfo = json.fromJson(postInfo,Map.class);

		// 文件名
		String filename = joPostInfo.get("filenameSaveAs") + ".xlsx"; // 设置下载时客户端Excel的名称
		fillWorkbook(workbook, joPostInfo);

		//处理中文文件名
		String reqCharset = req.getCharacterEncoding(); /*根据request的getCharacterEncoding得到请求时的编码*/  
		filename = new String(filename.getBytes(reqCharset), "ISO8859-1");   
		resp.setCharacterEncoding(reqCharset);
		// 若想下载时自动填好文件名，则需要设置响应头的"Content-disposition"
		resp.setHeader("Content-disposition", "attachment;filename=" + filename);
	}

	private void fillWorkbook(Workbook workbook, Map<String,String> joPostInfo) {
		// 标题栏
		SXSSFWorkbook sxssWb = (SXSSFWorkbook)workbook;
		Sheet sheet = sxssWb.createSheet("sheet1");
		int iniRow = 0;
		Row row;
		Cell cell;
		Gson gson = new Gson();
		if(null != joPostInfo.get("exportField")){
			// 含有标题栏
			List<String> jaTableField = gson.fromJson(joPostInfo.get("exportField").toString(),
					new TypeToken<List<String>>(){}.getType());
			String jaOneField;
			if(jaTableField.size() > 0){
				// 标题不为空
				row = sheet.createRow((short) iniRow);
				// 添加表格标题行
				for(int i = iniRow; i < jaTableField.size(); ++i){
					cell = row.createCell(i);
					jaOneField = gson.fromJson(gson.toJson(jaTableField.get(i)),
							String.class);
					cell.setCellType(CellType.STRING);
					cell.setCellValue(jaOneField);
				}
				++iniRow; // 标题行完成，下移一行
			}
			// 商品内容
			List<String> pdList = gson.fromJson(joPostInfo.get("exportContent").toString(),
					new TypeToken<List<String>>(){}.getType());
			// 商品的一行
			Map<String,String> jsOneRow = null;
			// 添加商品内容
			for(int pdRow = iniRow; pdRow < pdList.size() + iniRow; ++pdRow){
				row = sheet.createRow(pdRow);	// 在指定行创建一个Row对象
				jsOneRow = gson.fromJson(pdList.get(pdRow - iniRow).toString(),
						new TypeToken<Map<String, String>>(){}.getType());
				for(int col = 0; col < jaTableField.size(); ++col){
					// 循环填充一行的各列
					cell = row.createCell(col);
					cell.setCellType(CellType.STRING);
					// 取当前列的字段信息
					jaOneField = jaTableField.get(col);
					// 以字段代码为KEY,取JSONObject对象的值
					cell.setCellValue(jsOneRow.get(jaOneField));
				}
//				cell = row.createCell(col++);
//				cell.setCellValue(jsL.getString("styleCode"));
//				cell = row.createCell(col++);
//				cell.setCellValue(jsL.getString("pdName")); // 品名
//				cell = row.createCell(col++);
//				cell.setCellValue(jsL.getInt("length")); // 长度
//				cell = row.createCell(col++);
//				cell.setCellValue(jsL.getString("size")); // 尺码
//				cell = row.createCell(col++);
//				cell.setCellValue(jsL.getString("cusCode")); // 客户代码
//				cell = row.createCell(col++);
//				cell.setCellValue(jsL.getString("billNo")); // 单号
//				cell = row.createCell(col++);
//				cell.setCellValue(jsL.getString("status")); // 商品状态
				
			}
		}else{
			// 出错
		}
		
	}
	
	 
}
